package com.vercon.common.presenter;

import com.vercon.common.mode.common.BaseMode;
import com.vercon.common.mode.openweather.CityBean;
import com.vercon.common.mode.weather.AirAuality;
import com.vercon.common.mode.weather.JuHeIp;
import com.vercon.common.mode.weather.LocationIpCity;
import com.vercon.common.mode.weather.NewWeatherSimpleBean;
import com.vercon.common.mode.weather.NewWeatherSimpleLifeBean;
import com.vercon.common.network.KillerApi;
import com.vercon.common.network.MainApi;
import com.vercon.common.network.MyObserve;
import com.vercon.common.presenter.common.AbsPresenter;
import com.vercon.common.presenter.common.MirrorResultView;

import io.reactivex.disposables.Disposable;

/**
 * Created by csh on 2021/2/23
 */

public class MainPresenter extends AbsPresenter {

    public static final String GAO_WEATHER_URL = "https://restapi.amap.com/v3/";

    public static final String AIR_QUALITY_URL = "http://web.juhe.cn:8080/";
    public static final String AGGREGATION_URL = "http://v.juhe.cn/";//聚合天气接口
    public static final String CONTELLATION_URL = "http://web.juhe.cn:8080/constellation/";//获取星座
    public static final String CONSTELLATION_YELLOW_URL = "http://v.juhe.cn/laohuangli/";//获取黄历
    public static final String openweathermap_url = "http://api.openweathermap.org/";//openweather根据城市获取天气
    public static final String location_url = "https://ip.seeip.org/";//获取城市
    public static final String location_url2= "https://ip.nf/";//获取城市方式2
    public static final String juhe_aqi_url = "http://web.juhe.cn/";//获取聚合空气质量
    public static final String juhe_weather_warning_url = "http://apis.juhe.cn/";//星座
//    public static final String video_url = "http://service.i-mrmax.com/";//旧版视频列表

    public static final String weather_url = "http://v.juhe.cn/";//聚合天气接口
    //    public static final String gaode_weather_url = "https://restapi.amap.com/v3/";//高德天气接口
    public static final String huangli_url = "http://v.juhe.cn/laohuangli/";//黄历接口
    public static final String constellation_url = "http://web.juhe.cn:8080/constellation/";//星座
    public static final String juhe_ip = "http://apis.juhe.cn/";//聚合ip
    public static final String juhe_ip2 = "https://apis.juhe.cn/";//聚合ip


    public MainPresenter() {

    }



    public void getLocation(MirrorResultView<CityBean> resultView) {
        if (compositeDisposable == null){
            return;
        }
        MainApi.getInstance().getApi(location_url, KillerApi.class)
                .getLocation()
                .compose(schedulersTransformer())
                .subscribe(new MyObserve<CityBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);

                    }

                    @Override
                    public void onNext(CityBean fiveDayWeatherBean) {
                        if (null != resultView) {
                            if (null == fiveDayWeatherBean) {
                                resultView.onError(0, "没有数据");
                            } else
                                resultView.onSuccess(fiveDayWeatherBean);
                        }
                    }
                });
    }


    public void getLocation2(MirrorResultView<LocationIpCity> resultView) {
        if (compositeDisposable == null){
            return;
        }
        MainApi.getInstance().getApi(location_url2, KillerApi.class)
                .getLocation2()
                .compose(schedulersTransformer())
                .subscribe(new MyObserve<LocationIpCity>() {
                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);

                    }

                    @Override
                    public void onNext(LocationIpCity fiveDayWeatherBean) {
                        if (null != resultView) {
                            if (null == fiveDayWeatherBean) {
                                resultView.onError(0, "没有数据");
                            } else
                                resultView.onSuccess(fiveDayWeatherBean);
                        }
                    }
                });
    }


    public void getAqi(String cityName, MirrorResultView<BaseMode<AirAuality.ResultBean>> resultView) {
        if (compositeDisposable == null){
            return;
        }
        MainApi.getInstance().getApi(KillerApi.class).getAqi(cityName)
                .compose(schedulersTransformer())
                .subscribe(new MyObserve<BaseMode<AirAuality.ResultBean>>() {
                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.toString());
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseMode<AirAuality.ResultBean> data) {
                        if (null != resultView) {
                            if (null == data) {
                                resultView.onError(0, "no data");
                            } else
                                resultView.onSuccess(data);
                        }
                    }
                });
    }

    public void getJuheIp(String ip, MirrorResultView<JuHeIp> resultView) {
        if (compositeDisposable == null){
            return;
        }
        MainApi.getInstance().getApi(juhe_ip, KillerApi.class)
                .getJuheIp(ip)
                .compose(schedulersTransformer())
                .subscribe(new MyObserve<JuHeIp>() {
                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, e.getMessage());
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(JuHeIp o) {
                        resultView.onSuccess(o);
                    }
                });
    }

    /**简单的天气预报
     * @param resultView
     */
    public void getSimpleWeatherJuHe(String cityName, MirrorResultView<NewWeatherSimpleBean> resultView) {
        if (compositeDisposable == null){
            return;
        }
        MainApi.getInstance().getApi(KillerApi.class)
                .getSimpleWeatherJuHe(cityName)
                .compose(schedulersTransformer())
                .subscribe(new MyObserve<NewWeatherSimpleBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, "获取天气数据失败 " + e.getMessage());
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(NewWeatherSimpleBean o) {
                        resultView.onSuccess(o);
                    }
                });
    }

    public void getSimpleWeatherJuHeLife(String cityName, MirrorResultView<NewWeatherSimpleLifeBean> resultView) {
        if (compositeDisposable == null){
            return;
        }
        MainApi.getInstance().getApi(KillerApi.class)
                .getSimpleWeatherJuHeLife(cityName)
                .compose(schedulersTransformer())
                .subscribe(new MyObserve<NewWeatherSimpleLifeBean>() {
                    @Override
                    public void onNetError(Throwable e) {
                        resultView.onError(0, "获取天气数据失败 " + e.getMessage());
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(NewWeatherSimpleLifeBean o) {
                        resultView.onSuccess(o);
                    }
                });

    }



}